package se.thoka.twt.client.utils.validation;

import se.thoka.twt.client.utils.GWTDateTime;
import se.thoka.twt.client.utils.I18N;

/**
 * <p>
 * Asserts that the value is either <code>null</code> (empty) or can be parsed
 * into a date and time.
 * </p>
 * 
 * @author Thobias Karlsson, Thoka
 * @since 9 jan 2010
 */
public class DateTimeValidation implements Validation {
	
	public static final DateTimeValidation INSTANCE_ALLOW_NULL = new DateTimeValidation(true);
	public static final DateTimeValidation INSTANCE_NOT_NULL = new DateTimeValidation(false);

	private final String message;
	private final boolean allowNull;

	public DateTimeValidation(boolean allowNull) {
		this(allowNull, ValidationUtil.getStrings().dateTimeIncorrectFormatted(
				I18N.formatDateTime(new GWTDateTime(2010, 3, 26, 14, 32))));
	}

	public DateTimeValidation(boolean allowNull, String message) {
		this.allowNull = allowNull;
		this.message = message;
	}

	@Override
	public String getDescription() {
		return message;
	}

	@Override
	public boolean validate(String value) {
		return (value == null && allowNull) 
				|| I18N.tryParseDateTime(value) != null;
	}

}